package cn.gson.crmdemo.controller.privilege;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.gson.crmdemo.model.pojo.Dept;
import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.FunctionType;
import cn.gson.crmdemo.model.service.PrivilegeService;
import cn.gson.crmdemo.model.vo.EmpDeptVo;
import cn.gson.crmdemo.model.vo.EmpVo;

@Controller
public class EmpController {
	@Autowired
	PrivilegeService ps;
	/**
	 * 查询员工
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("emp-All")
	@ResponseBody
	public  Map<String, Object>  selectEmpAll(Integer page,Integer pageSize,Integer empId,
			String empName,Integer deptId) {
		Page<Object> pages = PageHelper.startPage(page,pageSize);
		//当前页的内容
		JSONArray list = ps.selectEmp(empId,empName,deptId);
		//总记录数
		long total =  pages.getTotal();
		Map<String, Object> map = new HashMap<>();
		if(deptId==null) {
			map.put("list", list);
			map.put("total", total);
		}
		else {
			map.put("list", list);
			map.put("total", total);
			map.put("deptxq", ps.selectDeptXq(deptId));
		}
		
		return map;
	}
	@RequestMapping("emp-Alls")
	@ResponseBody
	public  JSONArray selectEmpAlls(Integer empId,String empName,Integer deptId) {
		//当前页的内容
		JSONArray list = ps.selectEmp(empId,empName,deptId);
		return list;
	}
	/**
	 * 新增员工
	 * @param emp
	 */
	@RequestMapping("add-emp")
	public String addEmp(Emp emp) {
		boolean b = ps.addEmp(emp);
		if(b) {
			return "redirect:emp-All?page=1&pageSize=7&empName=";
		}
		return "sdfgf";
	}
	/**
	 * 修改员工信息
	 * @param emp
	 */
	@RequestMapping("edit-emp")
	public String updateEmp(EmpVo emp) {
		ps.updateEmp(emp);
		return "redirect:emp-All?page=1&pageSize=7&empName=";
	}
	/**
	 * 重置员工密码
	 * @return
	 */
	@RequestMapping("resets-emp")
	public String resets(Integer empId) {
		ps.resets(empId);
		return "redirect:emp-All?page=1&pageSize=7&empName=";
	}
	/**
	 * 查询员工上级
	 */
	@RequestMapping("emp-sj")
	@ResponseBody
	public JSONArray selectEmpSj(Integer empId) {
		return ps.selectEmpSj(empId);
	}

	/**
	 * 查询员工所有角
	 */
	@RequestMapping("emp-role")
	@ResponseBody
	public  List<Integer>  selectEmpRole(Integer empId) {
		return ps.selectRoleEmp(empId);
	}
	
	/**
	 * 员工离职	
	 */
	@RequestMapping("emp-quit")
	public  String  selectEmpQuit(Integer empId) {
		boolean b = ps.empQuit(empId);
		if(b) {
			return "redirect:emp-All?page=1&pageSize=7&empName=";
		}
		return "asdf";
	}

	/**
	 * 查询所有未分配部门的员工
	 */
	@RequestMapping("emp-deptisnull")
	@ResponseBody
	public JSONArray selectEmpNullDept() {
		return ps.selectEmpNullDept();
	}
	/**
	 * 给员工分配部门
	 */
	@RequestMapping("emp-dept")
	public String addEmpDept(@RequestBody List<EmpDeptVo> edvo) {
		ps.addEmpDept(edvo);
		return "redirect:dept-Alls";
	}
	/**
	 * 查询某个员工信息
	 */
	@RequestMapping("emp-info")
	@ResponseBody
	public JSONArray selectEmpInfo(HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return ps.selectEmpInfo(emp.getEmpId());
	}
	/**
	 * 修改个人密码
	 */
	@RequestMapping("update-empPass")
	@ResponseBody
	public int updateEmpPass(String pass1,String pass2,HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		boolean b = ps.updateEmpPass(pass1,pass2,emp.getEmpId());
		if(b) {
			return 0;
		}
		return 1;
	}
	/**
	 * 修改个人信息
	 */
	@RequestMapping("update-empInfo")
	public String updateEmpInfo(Emp emp,HttpServletRequest request) {
		Emp emp2 = (Emp) request.getSession().getAttribute("emp");
		ps.updateEmpInfo(emp,emp2.getEmpId());
		return "redirect:emp-info";
	}
	/**
	 * 查询某角色三级权限
	 */
	@RequestMapping("employee-qx")
	@ResponseBody
	public List<Integer> selectFuntionEmployee(HttpServletRequest request,Integer employeeId) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return ps.selectFuntionEmployee(emp.getEmpId(), employeeId);
	}
	/**
	 * 查询当前登陆人的最大角色等级
	 */
	@RequestMapping("emp-grade")
	@ResponseBody
	public Integer selectEmpGrade(HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return ps.selectEmpGrade(emp.getEmpId());
	}
}
